package main

import (
	"changeme/zerr"
	"log"
	"strconv"

	"github.com/syndtr/goleveldb/leveldb"
)

var db *leveldb.DB
var err error

func init() {
	db, err = leveldb.OpenFile("./leveldb", nil)
	if err != nil {
		log.Println(err)
	}
	log.Println(zerr.GetMsg(zerr.SUCCESS))
}

func DbGet(key string) (value string, zerrs int) {
	date, error := db.Get([]byte(key), nil)
	if error != nil {
		log.Println(err)
		return byteToString(date), zerr.ERROR
	}
	return byteToString(date), zerr.SUCCESS

}

func byteToString(b []byte) string {
	return string(b[:])
}

func DbPut(data map[string]string) int {
	for k, v := range data {
		e := db.Put([]byte(k), []byte(v), nil)

		if e != nil {
			log.Println("DbPut2: " + e.Error())
			return zerr.ERROR
		}
	}
	return zerr.SUCCESS
}

func GetWindowWidth() int {
	width, er := DbGet("window-width")
	if er != zerr.SUCCESS {
		log.Println("get width:" + zerr.GetMsg(er))
		return 1024
	}
	widthi, error := strconv.Atoi(width)
	if error != nil {
		log.Println(error)
		return 1024
	}
	return widthi

}

func GetWindowHeight() int {
	height, er := DbGet("window-height")
	if er != zerr.SUCCESS {
		log.Println("get height:" + zerr.GetMsg(er))
		return 1024
	}
	heighti, error := strconv.Atoi(height)
	if error != nil {
		log.Println(error)
		return 1024
	}
	return heighti

}
